<?php
	//set picture location off web root ../
	$strDefaultDirectory 	= "./images/product/";
	$strThumbzDirectory		= "./images/product/thumbz/";
	$strLargeDirectory		= "./images/product/large/";
	//Get Auction Item and copy to Product
	$strSQL = "INSERT INTO tblProduct
                     (     tblProduct.Product
                         , tblProduct.Description
                         , tblProduct.OwnProductCode
                         , tblProduct.Supplier
                         , tblProduct.QuantityPerUnit
                         , tblProduct.UnitPrice
						 , tblProduct.FullPrice
						 , tblProduct.VatRate
						 , tblProduct.MetaTitle
						 , tblProduct.MetaDescription
						 , tblProduct.MetaKeyword
						 , tblProduct.UploadedPicture
                     ) SELECT tblAuctionItem.ItemName	
                         , tblAuctionItem.Description
                         , tblAuctionItem.AuctionID
                         , '1'
                         , '1'
                         , tblAuctionItem.ReservePrice
						 , tblAuctionItem.ReservePrice
						 , '1'
						 , tblAuctionItem.ItemName
						 , tblAuctionItem.Description 
						 , tblAuctionItem.ItemName
						 , 'none'
						FROM tblAuctionItem
						WHERE tblAuctionItem.ID = ".$intAuctionItemID."
                       ";
    $strMessage = "Added To Shop";

    $strSQL .= ";";
	//echo $strSQL;

    if (!include (MODULE . "/action/actMySqlResults")){
    	array_push($ActionResults,"Failed To Add Product To Database");
		return(false);
    }
    
    if (!$intProductID){
		$intProductID = mysql_insert_id();
    }
    
	
	//e.g. item4003Photo1.jpg
	if(file_exists("./images/auction/thumbz/item".$intAuctionItemID."Photo1.jpg")){
		$strUploadedPicture = "item".$intAuctionItemID."Photo1.jpg";
		if(copy("./images/auction/thumbz/item".$intAuctionItemID."Photo1.jpg","./images/product/thumbz/product".$intProductID.".jpg"))
		{
			//Set uploaded photo name
			if ($intProductID){
        		$strSQL = "Update tblProduct
                     SET   tblProduct.UploadedPicture = 'product".$intProductID.".jpg'	"; 
        		$strJoin = " WHERE ";
        		$strSQL .= $strJoin."tblProduct.ID =  '$intProductID'";
        		$strMessage = "Photo name Updated";

        		if (!include (MODULE . "/action/actMySqlResults")){
    				array_push($ActionResults,"Failed To Add Product To Database");
					return(false);
    			}
			}	
		}else{
			echo "Thumbnail copy failed";
		}
	}
	
	if(CreateThumbNail($strThumbzDirectory,"product".$intProductID.$strPicture.".jpg",PRODUCT_THUMBNAIL_WIDTH)){
		array_push($ActionResults,"Thumbnail Creation Failed");
	}else{
		array_push($ActionResults,"thumbnail resize ok");
	}
	
	if(file_exists("./images/auction/item".$intAuctionItemID."Photo1.jpg")){
		if(copy("./images/auction/item".$intAuctionItemID."Photo1.jpg","./images/product/product".$intProductID.".jpg"))
		{
		}else{
			echo "main copy failed";	
		}
	}
	
	// Shrink copied picture
	if(CreateThumbNail($strDefaultDirectory,"product".$intProductID.$strPicture.".jpg",PRODUCT_PICTURE_WIDTH)){
		array_push($ActionResults,"Resize of ".PRODUCT_PICTURE_WIDTH." pixel picture failed");
	}else{
		array_push($ActionResults,"default resize ok");
	}
	
	$intCount = 1;
	do{
		$intCount++;
		$strPicture = "picture".$intCount;
		
		if(file_exists("./images/auction/thumbz/item".$intAuctionItemID."Photo$intCount.jpg")){			
			if(copy("./images/auction/thumbz/item".$intAuctionItemID."Photo$intCount.jpg",$strThumbzDirectory."product".$intProductID.$strPicture.".jpg")){
				if(CreateThumbNail($strThumbzDirectory,"product".$intProductID.$strPicture.".jpg",PRODUCT_THUMBNAIL_WIDTH)){
					array_push($ActionResults,"Thumbnail Creation Failed");
				}else{
					array_push($ActionResults,"thumbnail resize ok");
				}
			}else{
				echo "Thumbnail copy failed";
			}
		}
	
		if(file_exists("./images/auction/item".$intAuctionItemID."Photo$intCount.jpg")){
			if(copy("./images/auction/item".$intAuctionItemID."Photo$intCount.jpg",$strDefaultDirectory."product".$intProductID.$strPicture.".jpg")){			
				// Shrink copied pictures
				if(CreateThumbNail($strDefaultDirectory,"product".$intProductID.$strPicture.".jpg",PRODUCT_PICTURE_WIDTH)){
					array_push($ActionResults,"Resize of ".PRODUCT_PICTURE_WIDTH." pixel picture failed");
				}else{
					array_push($ActionResults,"default resize ok");
				}
			}else{
				echo "main copy failed";	
			}
		}	
		
		
		
		
	}while($intCount < 10)

?>